//
//  RootTabBarViewController.m
//  jobClient
//
//  Created by 一览ios on 16/7/14.
//  Copyright © 2016年 YL1001. All rights reserved.
//

#import "RootTabBarViewController.h"
#import "MessageCenterList.h"

#import "TodayFocusListCtl.h"
#import "TabBarView.h"
#import "RootNavigationController.h"
#import "MyJobGuideCtl.h"
#import "AssociationAppDelegate.h"
#import "IndustryViewController.h"
#import "ELStudentRecruitCtl.h"
#import "ELHomePageJobCtl.h"

#import "ELFellowTravelerListCtl.h"

#import "ELNewIndustryAskedCtl.h"
#import "ELTalentMessageListCtl.h"
#import "ELIndustryAskedCtl.h"
#import "ELMyResumeCtl.h"
#import "ELMineCtl.h"
#import "ELCareerGuideCtl.h"

#import "ELHomeJobCtl.h"
#import "ELRegisterWelcomeCtl.h"
#import "ELChooseIdentifyCtl.h"
#import "ELNewHomePageRootCtl.h"
#import "ELFindCtl.h"
#import "ELCareerEncyclopediaCtl.h"

@interface RootTabBarViewController ()<TabBarDelegate>{
    TabBarView *myTabBar;
    NSArray *tabCtlArr;
}

@property(nonatomic,assign)CGFloat tabBarHeight;

@end

@implementation RootTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self registerNotify];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (tabCtlArr && self.selectedIndex < tabCtlArr.count){
        [Manager shareMgr].centerNav_ = tabCtlArr[self.selectedIndex];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    if ([Manager shareMgr].haveLogin) {
        [Manager shareMgr].uuidHaveLogin = NO;
        [Manager shareMgr].uuidPersonid = nil;
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - 配置UI
- (void)configUI {
    [[UITabBar appearance] setTranslucent:YES];

    tabCtlArr = @[[self getHomePath],
                  [self getReportPath],
                  [self getMessagePath],
                  [self getMinePath]];
    self.viewControllers = tabCtlArr;

    [Manager shareMgr].centerNav_ = tabCtlArr[0];
    [self configTabBar];
}

//配置tabbar
- (void)configTabBar {
    [self.tabBar setTranslucent:YES];  // view是否会延伸到最底部
    
    [self.tabBar setShadowImage:[[UIImage alloc] init]];
    [self.tabBar setBackgroundImage:[[UIImage alloc] init]];

    myTabBar = [[TabBarView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ToolBarHeight) controllersNum:(int)tabCtlArr.count];
    [self.tabBar addSubview:myTabBar];
    self.tabBarView = myTabBar;
    myTabBar.delegate = self;
//    [self setTabBarLine];

}

//配置tabbar顶部线条
//-(void)setTabBarLine
//{
//    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.5)];
//    lineLabel.backgroundColor = UIColor.clearColor;//EL_TabBarLineColor
//    [myTabBar addSubview:lineLabel];
//}


// 切换到第一个tab
//- (void)loginSuccessReloadTabBarViewController {
//    for (RootNavigationController *nav in tabCtlArr) {
//        [nav popToRootViewControllerAnimated:NO];
//    }
//    
//    [self configTabBar];
//    [Manager shareMgr].centerNav_ = [tabCtlArr firstObject];
//    [self showControllerIndex:0];
//}

#pragma mark - ---------->第一次注册欢迎页
//-(void)gotoRegisterWelcome {
//    ELChooseIdentifyCtl *welcomeCtl = [[ELChooseIdentifyCtl alloc] init];
//    welcomeCtl.hidesBottomBarWhenPushed = YES;
//    [[Manager shareMgr].centerNav_ pushViewController:welcomeCtl animated:YES];
//    [Manager shareMgr].registeLoginUserId = nil;
//}

#pragma mark - TabBarDelegate
-(void)buttonSelectedClick:(UIButton *)button{
    NSInteger index = button.tag-1000;
    [self showControllerIndex:index];
    
//    NSString *name;
//    switch (index) {
//        case 1:
//            name = @"Navigation_bar_Careers";  // 职业
//            break;
//        case 2:
//            name = @"Navigation_bar_Home_Page";  // 首页
//            break;
//        case 3:
//            name = @"Navigation_bar_Messages";  // 消息
//            break;
//        case 4:
//            name = @"The_navigation_bar._My";  // 我的
//            break;
//        default:
//            name = @"Navigation_bar_Job_hunting";  // 找工作
//            break;
//    }
//    [ELMobClick event:name];
}

#pragma mark - public method

//切换显示控制器
-(void)showControllerIndex:(NSInteger)index
{
    self.seleBtn.selected = NO;
    UIButton *button = (UIButton *)[myTabBar viewWithTag:1000+index];
    button.selected = YES;
    self.seleBtn = button;
    self.selectedIndex = index;
    [Manager shareMgr].centerNav_ = tabCtlArr[index];
    
}

-(UINavigationController *)getCurrentNavCtl{
    return tabCtlArr[self.selectedIndex];
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    if (selectedIndex >= self.viewControllers.count) {
        return;
    }

    if (selectedIndex == self.viewControllers.count - 1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ReflashToRecruitAnimationNotification" object:nil];
    }
    
    for (NSInteger i=0;i<tabCtlArr.count;i++) {
        UIButton *button = (UIButton *)[myTabBar viewWithTag:1000+i];
        button.selected = NO;
    }
    super.selectedIndex = selectedIndex;
    UIButton *button = (UIButton *)[myTabBar viewWithTag:1000+selectedIndex];
    button.selected = YES;
    self.seleBtn = button;
    [Manager shareMgr].centerNav_ = tabCtlArr[selectedIndex];
}



#pragma mark - 通知
/**
 *  tabbar上面消息红点提示
 */
-(void)registerNotify {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(numMsgShow:) name:@"msgNewsNum" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(numMyManagerShow:) name:@"myManagerNum" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearBadge:) name:@"RootTabBarClearBadgeNotification" object:nil];
}
//消息 红点
-(void)numMsgShow:(NSNotification *)notify{//工作简历相关消息数 + 社区消息数 + 极光聊天消息数
    NSLog(@"----------------%@",kAllNums);
    NSInteger jobMsgNum = [getUserDefaults(kAllJobNums) integerValue];
    NSInteger allNumber = [getUserDefaults(kAllUnreadCount) integerValue] + jobMsgNum;//V7.0删除社区消息红点计算
    [myTabBar showBadgeMark:allNumber index:tabCtlArr.count-2];
}
//我的 红点
-(void)numMyManagerShow:(NSNotification *)notify{
    NSDictionary *dic = notify.userInfo;
    NSInteger otherAllNum = [dic[@"num"] integerValue];
    NSInteger oaNum = [dic[@"oaNum"] integerValue];
    
    NSInteger allNum = otherAllNum + oaNum;
    [myTabBar showBadgeMark:allNum index:tabCtlArr.count-1];
}

- (void)clearBadge:(NSNotification *)notify {
    for (NSInteger i = 0; i < tabCtlArr.count; i++) {
        [myTabBar showBadgeMark:0 index:i];
    }
}

#pragma mark -


// 找工作
- (RootNavigationController *)getHomePath {
    // v9.x版本(二级版本)
    ELHomePageJobCtl *jobCtl = [[ELHomePageJobCtl alloc] init];
    return [[RootNavigationController alloc] initWithRootViewController:jobCtl];
    
    // v8.x版本(三级版本 v9.0弃用)
    //ELHomeJobCtl *jobCtl = [[ELHomeJobCtl alloc] init];
    //return [[RootNavigationController alloc] initWithRootViewController:jobCtl];
}


// 发现(职讯、短视频、职播)
- (RootNavigationController *)getReportPath {
    ELCareerGuideCtl *ctl = [[ELCareerGuideCtl alloc] init];
    return [[RootNavigationController alloc] initWithRootViewController:ctl];
}

// 消息
- (RootNavigationController *)getMessagePath {
    ELTalentMessageListCtl *ctl = [[ELTalentMessageListCtl alloc] init];
    return [[RootNavigationController alloc] initWithRootViewController:ctl];
    
    // 腾讯带UI的IM页面（2025年2月 弃用）
    //    TXIMPersonConversationListCtl *msgVC = [[TXIMPersonConversationListCtl alloc] init];
    //    RootNavigationController *msgNav = [[RootNavigationController alloc] initWithRootViewController:msgVC];
}

// 我的
- (RootNavigationController *)getMinePath {
    ELMineCtl *ctl = [[ELMineCtl alloc] init];
    return [[RootNavigationController alloc] initWithRootViewController:ctl];
}

// 求职（职讯，v9.0弃用）
//- (RootNavigationController *)getNewsPath {
//    ELNewHomePageRootCtl *ctl = [[ELNewHomePageRootCtl alloc] init];
//    return [[RootNavigationController alloc] initWithRootViewController:ctl];
//}

// v9.0 职业大全 (2025年4月30 没上线、暂停使用)
//- (RootNavigationController *)getCareerPath {
//    ELCareerEncyclopediaCtl *ctl = [[ELCareerEncyclopediaCtl alloc] initWithNibName:@"ELCareerEncyclopediaCtl" bundle:nil];
//    return [[RootNavigationController alloc] initWithRootViewController:ctl];
//}

//v9.0发现 (2025年4月30 没上线、暂停使用)
//- (RootNavigationController *)getFindPath {
//    ELFindCtl *ctl = [[ELFindCtl alloc] init];
//    return [[RootNavigationController alloc] initWithRootViewController:ctl];
//}




@end
